﻿using System;
using System.ComponentModel;
using System.Windows;
using DevEnverModal;
using QuailDashboard.View.Helpers;
using QuailDashboard.View.Models;
using QuailDashboard.View.QuailServiceReference;
using QuailDashboard.View.Views;

namespace QuailDashboard.View.Controls
{
    public partial class AddEnvironmentControl
    {
        private readonly PageState _pageState = new PageState();
        private readonly ModalControl _modalControl = new ModalControl();

        public AddEnvironmentControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the ModalControl manager for this instance
        /// </summary>
        public ModalControl Modal
        {
            get;
            set;
        }

        public TestEnvironment Environment
        {
            get
            {
                return _pageState.Environment;
            }

            set
            {
                _pageState.Environment = value;
            }
        }

        public event EventHandler Finished;

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = _pageState;
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            FireFinished();
        }

        private void ButtonUpdate_Click(object sender, RoutedEventArgs e)
        {
            var service = QuailDataServiceFactory.CreateClient();
            service.UpdateTestEnvironmentCompleted += OnUpdateCompleted;
            service.UpdateTestEnvironmentAsync(_pageState.Environment.TestEnvironmentId, _pageState.EnvironmentName, _pageState.EnvironmentDescription);
            _pageState.IncProcessing("Updating...");
        }

        private void OnUpdateCompleted(object sender, AsyncCompletedEventArgs e)
        {
            _pageState.DecProcessing();
            if (e.Error != null)
            {
                App.CurrentApp.ShowError(e.Error);
            }
            else
            {
                FireFinished();
            }
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var service = QuailDataServiceFactory.CreateClient();
            service.AddTestEnvironmentCompleted += OnAddCompleted;
            service.AddTestEnvironmentAsync(_pageState.EnvironmentName, _pageState.EnvironmentDescription);
            _pageState.IncProcessing("Adding...");
        }

        private void OnAddCompleted(object sender, AddTestEnvironmentCompletedEventArgs e)
        {
            _pageState.DecProcessing();
            if (e.Error != null)
            {
                App.CurrentApp.ShowError(e.Error);
            }
            else
            {
                FireFinished();
            }            
        }

        private void FireFinished()
        {
            Modal.HideModal();
            if (Finished != null)
                Finished(this, EventArgs.Empty);
        }

        private void ButtonConfigure_Click(object sender, RoutedEventArgs e)
        {
            var ctrl = new ConfigureEnvironmentControl
                           {
                               Modal = _modalControl,
                               Environment = Environment
                           };
            _modalControl.ShowModal(ctrl);
        }

        public class PageState : IsProcessingStateModel
        {
            private bool _editMode;
            private TestEnvironment _environment;
            private string _environmentName, _environmentDesc;

            public string Caption
            {
                get
                {
                    return EditMode ? "Edit Environment" : "Add New Environment";
                }
            }

            public bool EditMode
            {
                get
                {
                    return _editMode;
                }

                set
                {
                    if (value != _editMode)
                    {
                        _editMode = value;
                        NotifyPropertyChanged("EditMode");
                        NotifyPropertyChanged("Caption");
                    }
                }
            }

            public TestEnvironment Environment
            {
                get
                {
                    return _environment;
                }

                set
                {
                    _environment = value;
                    EditMode = value != null;
                    NotifyPropertyChanged("Environment");
                }
            }

            public string EnvironmentName
            {
                get
                {
                    if (_environment != null && _environmentName == null)
                        _environmentName = _environment.EnvironmentName;

                    return _environmentName;
                }

                set
                {
                    _environmentName = value;
                    NotifyPropertyChanged("EnvironmentName");
                }
            }

            public string EnvironmentDescription
            {
                get
                {
                    if (_environment != null && _environmentDesc == null)
                        _environmentDesc = _environment.Description;

                    return _environmentDesc;
                }

                set
                {
                    _environmentDesc = value;
                    NotifyPropertyChanged("EnvironmentDescription");
                }
            }
        }
    }
}
